﻿#define Debug

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using StockClientBll;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Common.Models;


namespace StockClientUI
{
    public partial class Login : Form
    {
        
        public bool CanLogin = false;           //记录是否通过检验
        public  DataSet ds = new DataSet();
        public static int Times = 0;           
     
        //UserInfoBll userinfobll = new UserInfoBll();

      

       
     
    
        public Login()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = Application.StartupPath + @"\DiamondGreen.ssk";
        }
        ClientConnector m_connector;
        private void Login_Load(object sender, EventArgs e)
        {
             this.cboLanguage.Text = "(简体)中文";
             ReadIni readini = new ReadIni(Application.StartupPath + @"\\Config.ini");
             if (readini.ExistINIFile())
             {
                
               
                 try
                 {
                   m_connector=  ClientConnector.GetInstance();
                   m_connector.ErrorEvt += new OnError(m_connector_ErrorEvt);
                   m_connector.RecivedMsgEvt += new OnRecivedMsgDelegate(m_connector_RecivedMsgEvt);
                   m_connector.Init();
                 //    socket.Send();
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.ToString());
                 }
             }
             else
             {
                 MessageBox.Show("File is not exist!");
                 this.Close();
             }
           
        }

        void m_connector_RecivedMsgEvt(string msg)
        {
            MessageBox.Show("RecivedMsgEvt  :  " + msg);
        }

        void m_connector_ErrorEvt(string errorMsg)
        {
            MessageBox.Show(errorMsg);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.CanLogin = false;
            this.Close();
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.txtAcNo.Text.Trim() == "" || this.txtPassword.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名和密码");
                return;
            }
            string getuserCn = txtAcNo.Text;
            string getpawd = txtPassword.Text;
            string totalstring =  getuserCn + "#" + getpawd;

            try
            {
              
               ClientConnector.GetInstance().Send( totalstring,MsgType.Login);
               ClientConnector.BeginAcceptData();
              
            }
            catch (Exception ex)
            {
                ex.GetBaseException();
            }

            //    sendDone.WaitOne();

            #region 登录后的判断 
            //while (socket.Connected)
            //{
            //    Receive(socket);
            //    // 登录成功
            //    if (response.Contains("success"))
            //    {
                  //string []getstringinfo= response.Split('@');
                  //string getxml=getstringinfo[1].ToString();
                  //FileInfo fo = new FileInfo(Application.StartupPath + @"\Mytest.xml");
                  //fo.Delete();
                  //FileStream fs = new FileStream(Application.StartupPath + @"\Mytest.xml",FileMode.OpenOrCreate,FileAccess.Write);               
                  //StreamWriter sw = new StreamWriter(fs);
                  //sw.WriteLine(getxml);
                  //sw.Flush();
                  //sw.Close(); 
                  //fs.Close();
                
                  //FileStream myfs = new FileStream(Application.StartupPath + @"\Mytest.xml", FileMode.Open, FileAccess.Read);
                  //StreamReader myreader = new StreamReader(myfs);
                  //ds.ReadXml(myreader);
                  //myfs.Close();

                  //  MessageBox.Show("成功登录");
                  //  if (getreceive != null)
                  //  {
                  //      getreceive.Abort();
                    
                  //  }
                  //  CanLogin = true;
                  //  ClientModel.GetUserInfo().Guid = "sdafasdfasfweqefwq";
                  //  ClientModel.GetUserInfo().Password = "";
                  //  ClientModel.GetUserInfo().UserName = "";
                  //  this.Close();
                  //  return;
                //}
                //登录失败
               //if (response.Contains("failuser"))
               // {
               //     MessageBox.Show("登录失败");
               //     if (getreceive != null)
               //     {
               //         getreceive.Abort();

               //     }
               //     CanLogin = false;
               //     this.Close();
               //     return;                 
               // }
               //receiveDone.WaitOne();
           //}
            #endregion


       }

            //socket.Shutdown(SocketShutdown.Both);
            //socket.Close();




        private void Login_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        
    }
}